﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yunchang.NetworkLayer;

/// <summary>
/// From：张帅
/// PS：自走棋战斗结果界面
/// </summary> 
namespace UI
{
    public class ChessResultPage : OPPage
    {
        public ChessResultItem myPref, enermyPref;
        public Transform myParent, enermyParent, effectParent, coinPos;
        public float closeTime = 2f;

        public GameObject goldItem;
        public Text addGoldNum;
        float currTime;
        protected override void DoOpen()
        {
            base.DoOpen();

            var msg = Params as SCAutoChessRoundFightResult;
            if (msg != null)
            {
                Init(msg);
                currTime = Time.time;
                AutoChess.AutoChessManager.Ins.cacheData.fighResult = null;
            }
            NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCAutoChessGetFightSettle, OnFinishFightGold);

            NetworkDriver.Instance.SendSocketBlock(OpDefine.CSAutoChessGetFightSettle, new CSAutoChessGetFightSettle(), OpDefine.SCAutoChessGetFightSettle);
        }

        protected override void DoClose(bool immediately)
        {
            base.DoClose(immediately);
            NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCAutoChessGetFightSettle, OnFinishFightGold);
        }
        private void OnFinishFightGold(object msg)
        {
            var data = msg as SCAutoChessGetFightSettle;
            if (data != null)
            {
                bool isNeedShow = data.gold > 0;
                goldItem.SetActiveEx(isNeedShow);
                if (isNeedShow)
                {
                    addGoldNum.text = "+" + data.gold.ToString();
                    var startScreenPos = RectTransformUtility.WorldToScreenPoint(CameraTools.Instance._uiCamera, goldItem.transform.position);
                    var endScreenPos = RectTransformUtility.WorldToScreenPoint(CameraTools.Instance._uiCamera, coinPos.position);
                    Vector2 startPos, endPos;
                    RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.GetComponent<RectTransform>(), startScreenPos, CameraTools.Instance._uiCamera, out startPos);
                    RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.GetComponent<RectTransform>(), endScreenPos, CameraTools.Instance._uiCamera, out endPos);
                    MeteorEffect.GenMeteorEffect(transform, startPos, endPos, new Vector3(300, 0, 0));
                }
            }
        }

        private void Init(SCAutoChessRoundFightResult msg)
        {
            AutoChess.UtilityHelper.PlayUIAudio("AC_winning");
            CommonEffectResId resId = msg.win ? CommonEffectResId.UnionSeaFight_ResultPage_ShengLi : CommonEffectResId.UnionSeaFight_ResultPage_ShiBai;
            CommonEffectManager.Instance.PlayEffectImmediate(resId, effectParent);

            UpdateList(msg.myChess, true);
            UpdateList(msg.enemyChess, false);
        }

        void UpdateList(List<AutoChessFightHeroInfo> list, bool isMySelf)
        {
            if (list.Count <= 0)
                return;
            list.Sort((AutoChessFightHeroInfo a, AutoChessFightHeroInfo b)=>{
                return (int)(b.demage - a.demage);
            });
            double maxDemage = list[0].demage;
            var pref = isMySelf ? myPref : enermyPref;

            pref.gameObject.SetActive(true);
            for (int i = 0; i < list.Count; i++)
            {
                var item = Instantiate(pref);
                TransformUtility.SetParent(item.transform,isMySelf ? myParent : enermyParent);
                item.SetItem(list[i], maxDemage);
            }
            pref.gameObject.SetActive(false);
        }
    }
}
